package cn.com.woxitech.dev.common;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class JSONUtils {
	public static final ObjectMapper mapper = new ObjectMapper();
	public static JsonNode toJson(String jsonstr) throws JsonProcessingException, IOException {
		// TODO Auto-generated method stub
		return mapper.readTree(jsonstr);
	}

	public static Object toList(String jsonstr, Class<?> collectionClass, Class<?>... elementClasses) throws JsonParseException, JsonMappingException, IOException {
		JavaType javaType = mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
		return mapper.readValue(jsonstr, javaType);
	}

	public static ObjectNode jsonInit() {
		// TODO Auto-generated method stub
		return mapper.createObjectNode();
	}

	public static ArrayNode arrayInit() {
		// TODO Auto-generated method stub
		return mapper.createArrayNode();
	}
}
